package com.example.demo.controller;

import com.example.demo.dao.CustomerDao;
import com.example.demo.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class CustomerController {
    @Autowired
    private CustomerDao customerDao;

    // 自动获取参数
    @RequestMapping("/add/auto")
    @ResponseBody
    public Customer addByAuto(String jobNo, String name, String department) {
        Customer customer = new Customer(null, jobNo, name, department);
        return customerDao.save(customer);
    }

    // 使用@RequestParam获取参数
    @RequestMapping("/add/param")
    @ResponseBody
    public Customer addParam(String jobNo, @RequestParam("name") String username, String department) {
        Customer customer = new Customer(null, jobNo, username, department);
        return customerDao.save(customer);
    }

    // 使用@RequestBody获取参数
    @RequestMapping("/add/body")
    @ResponseBody
    public Customer addBody(@RequestBody Customer customer) {
        return customerDao.save(customer);
    }

    // 使用@ModelAttribute获取参数
    @RequestMapping("/add/model")
    @ResponseBody
    public Customer addModel(@ModelAttribute("customer") Customer customer) {
        return customerDao.save(customer);
    }

    // 使用@PathVariable获取参数
    @RequestMapping("/query/{id}")
    @ResponseBody
    public Customer queryPath(@PathVariable("id") Long id) {
        return customerDao.findById(id).orElse(null);
    }
}
